{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# HTML解析入门及准备URL生成连续技\n",
    "![for humans](https://requests-html.kennethreitz.org/_static/requests-html-logo.png#thumbnail)\n",
    "\n",
    "*  本周主要内容：批量抓取页面基础及技巧\n",
    "*  上周主要内容：HTML解析（parse HTML）及准备URL生成连续技\n",
    "*  20春_Web数据挖掘_week04\n",
    "*  电子讲义设计者：廖汉腾, 许智超\n",
    "*  修改讲义者：黄苑婷\n",
    "<br/>\n",
    "<br/>\n",
    "\n",
    "-----\n",
    "## 复习\n",
    "\n",
    "复习：上周内容，实践\n",
    "\n",
    "* 猎聘PC版 liepin.com 取工作URL参数的牛肉\n",
    "* 如何生成一连串新URL以进一步爬取数据\n",
    "\n",
    "\n",
    "-----\n",
    "## 本周内容及学习目标\n",
    "\n",
    "本周内容聚焦在\n",
    "\n",
    "<mark> 如何有系统的把更多页数据(相同结构)作系统性爬取 </mark>\n",
    "\n",
    "为此，我们需要学习\n",
    "\n",
    "* 翻页：参数字典的拆解\n",
    "  * xpath\n",
    "  * 建构参数模板\n",
    "  * 建构参数字典\n",
    "* 翻页：系统性迭代\n",
    "  * robots.txt\n",
    "  * 频率及时间\n",
    "* 翻页：数据备份与整合\n",
    "  * 储存备份\n",
    "  * 数据整合\n",
    "  \n",
    "### 目标\n",
    "1. 使用 requests-html 爬取并存取网页文字档，查找[requests-html 中文文档](https://cncert.github.io/requests-html-doc-cn/#/)\n",
    "2. 熟悉 [xpath 语法](https://www.w3cschool.cn/xpath/xpath-syntax.html)丶[xpath 节点](https://www.w3cschool.cn/xpath/xpath-nodes.html)\n",
    "3. 使用 [xpath cheatsheet](https://devhints.io/xpath)\n",
    "  * 在 Chrome Inspector 使用\n",
    "  * 在 requests-html (Python) 使用\n",
    "4. 简易使用 [pd.DataFrame](https://www.pypandas.cn/doc/getting_started/dsintro.html#dataframe)\n",
    "5. 参数字典的拆解与迭代\n",
    "6. 翻页数据备份与整合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>\n",
       "/* 本电子讲义使用之CSS */\n",
       "div.code_cell {\n",
       "    background-color: #e5f1fe;\n",
       "}\n",
       "div.cell.selected {\n",
       "    background-color: #effee2;\n",
       "    font-size: 2rem;\n",
       "    line-height: 2.4rem;\n",
       "}\n",
       "div.cell.selected .rendered_html table {\n",
       "    font-size: 2rem !important;\n",
       "    line-height: 2.4rem !important;\n",
       "}\n",
       ".rendered_html pre code {\n",
       "    background-color: #C4E4ff;   \n",
       "    padding: 2px 25px;\n",
       "}\n",
       ".rendered_html pre {\n",
       "    background-color: #99c9ff;\n",
       "}\n",
       "div.code_cell .CodeMirror {\n",
       "    font-size: 2rem !important;\n",
       "    line-height: 2.4rem !important;\n",
       "}\n",
       ".rendered_html img, .rendered_html svg {\n",
       "    max-width: 60%;\n",
       "    height: auto;\n",
       "    float: right;\n",
       "}\n",
       "\n",
       ".rendered_html img[src*=\"#full\"], .rendered_html svg[src*=\"#full\"] {\n",
       "    max-width: 100%;\n",
       "    height: auto;\n",
       "    float: none;\n",
       "}\n",
       "\n",
       ".rendered_html img[src*=\"#thumbnail\"], .rendered_html svg[src*=\"#thumbnail\"] {\n",
       "    max-width: 15%;\n",
       "    height: auto;\n",
       "}\n",
       "\n",
       "/* Gradient transparent - color - transparent */\n",
       "hr {\n",
       "    border: 0;\n",
       "    border-bottom: 1px dashed #ccc;\n",
       "}\n",
       ".emoticon{\n",
       "    font-size: 5rem;\n",
       "    line-height: 4.4rem;\n",
       "    text-align: center;\n",
       "    vertical-align: middle;\n",
       "}\n",
       ".bg-split_apply_comine {\n",
       "    width: 500px;     \n",
       "    height: 300px;\n",
       "    background: url('02_split-apply-comine_500x300.png') -10px -10px;\n",
       "    float: right;\n",
       "}\n",
       ".bg-comine {\n",
       "    width: 175px;\n",
       "    height: 150px;\n",
       "    background: url('02_split-apply-comine_500x300.png') -280px -80px;\n",
       "    float: right;\n",
       "}\n",
       ".bg-apply {\n",
       "    width: 155px;\n",
       "    height: 225px;\n",
       "    background: url('02_split-apply-comine_500x300.png') -160px -30px;\n",
       "    float: right;\n",
       "}\n",
       ".bg-split {\n",
       "    width: 205px;\n",
       "    height: 225px;\n",
       "    background: url('02_split-apply-comine_500x300.png') -10px -30px;\n",
       "    float: right;\n",
       "}\n",
       ".break {\n",
       "                   page-break-after: right; \n",
       "                   width:700px;\n",
       "                   clear:both;\n",
       "}\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<style>\n",
    "/* 本电子讲义使用之CSS */\n",
    "div.code_cell {\n",
    "    background-color: #e5f1fe;\n",
    "}\n",
    "div.cell.selected {\n",
    "    background-color: #effee2;\n",
    "    font-size: 2rem;\n",
    "    line-height: 2.4rem;\n",
    "}\n",
    "div.cell.selected .rendered_html table {\n",
    "    font-size: 2rem !important;\n",
    "    line-height: 2.4rem !important;\n",
    "}\n",
    ".rendered_html pre code {\n",
    "    background-color: #C4E4ff;   \n",
    "    padding: 2px 25px;\n",
    "}\n",
    ".rendered_html pre {\n",
    "    background-color: #99c9ff;\n",
    "}\n",
    "div.code_cell .CodeMirror {\n",
    "    font-size: 2rem !important;\n",
    "    line-height: 2.4rem !important;\n",
    "}\n",
    ".rendered_html img, .rendered_html svg {\n",
    "    max-width: 60%;\n",
    "    height: auto;\n",
    "    float: right;\n",
    "}\n",
    "\n",
    ".rendered_html img[src*=\"#full\"], .rendered_html svg[src*=\"#full\"] {\n",
    "    max-width: 100%;\n",
    "    height: auto;\n",
    "    float: none;\n",
    "}\n",
    "\n",
    ".rendered_html img[src*=\"#thumbnail\"], .rendered_html svg[src*=\"#thumbnail\"] {\n",
    "    max-width: 15%;\n",
    "    height: auto;\n",
    "}\n",
    "\n",
    "/* Gradient transparent - color - transparent */\n",
    "hr {\n",
    "    border: 0;\n",
    "    border-bottom: 1px dashed #ccc;\n",
    "}\n",
    ".emoticon{\n",
    "    font-size: 5rem;\n",
    "    line-height: 4.4rem;\n",
    "    text-align: center;\n",
    "    vertical-align: middle;\n",
    "}\n",
    ".bg-split_apply_comine {\n",
    "    width: 500px;     \n",
    "    height: 300px;\n",
    "    background: url('02_split-apply-comine_500x300.png') -10px -10px;\n",
    "    float: right;\n",
    "}\n",
    ".bg-comine {\n",
    "    width: 175px;\n",
    "    height: 150px;\n",
    "    background: url('02_split-apply-comine_500x300.png') -280px -80px;\n",
    "    float: right;\n",
    "}\n",
    ".bg-apply {\n",
    "    width: 155px;\n",
    "    height: 225px;\n",
    "    background: url('02_split-apply-comine_500x300.png') -160px -30px;\n",
    "    float: right;\n",
    "}\n",
    ".bg-split {\n",
    "    width: 205px;\n",
    "    height: 225px;\n",
    "    background: url('02_split-apply-comine_500x300.png') -10px -30px;\n",
    "    float: right;\n",
    "}\n",
    ".break {\n",
    "                   page-break-after: right; \n",
    "                   width:700px;\n",
    "                   clear:both;\n",
    "}\n",
    "</style>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基本模块\n",
    "import pandas as pd\n",
    "from requests_html import HTMLSession"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0. 上周整合代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "edu         5\n",
      "经验         10\n",
      "薪水         66\n",
      "时间         24\n",
      "职称        181\n",
      "公司地点       69\n",
      "公司名称       70\n",
      "链结        191\n",
      "公司URL      70\n",
      "热门公司类型      6\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>职称</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司名称</th>\n",
       "      <th>edu</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>腾讯</th>\n",
       "      <th>本科及以上</th>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>华为</th>\n",
       "      <th>本科及以上</th>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>小米</th>\n",
       "      <th>统招本科</th>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>明略科技集团</th>\n",
       "      <th>统招本科</th>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>腾讯</th>\n",
       "      <th>学历不限</th>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>招金矿业</th>\n",
       "      <th>统招本科</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>新城悦控股有限公司</th>\n",
       "      <th>大专及以上</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>新城控股集团住宅开发事业部</th>\n",
       "      <th>本科及以上</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>方太</th>\n",
       "      <th>本科及以上</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>戴维医疗</th>\n",
       "      <th>统招本科</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>93 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     职称\n",
       "公司名称          edu      \n",
       "腾讯            本科及以上  34\n",
       "华为            本科及以上  12\n",
       "小米            统招本科   12\n",
       "明略科技集团        统招本科   11\n",
       "腾讯            学历不限    9\n",
       "...                  ..\n",
       "招金矿业          统招本科    1\n",
       "新城悦控股有限公司     大专及以上   1\n",
       "新城控股集团住宅开发事业部 本科及以上   1\n",
       "方太            本科及以上   1\n",
       "戴维医疗          统招本科    1\n",
       "\n",
       "[93 rows x 1 columns]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 上周C-1B-5 建构 参数模板  \n",
    "参数_compTag_用户体验 = {'中国500强': {'init': ['-1'], 'headckid': ['58d828c357a8cb19'], 'flushckid': ['1'], 'fromSearchBtn': ['2'], 'keyword': ['用户体验'], 'compTag': ['155'], 'ckid': ['58d828c357a8cb19'], 'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'], 'd_sfrom': ['search_unknown'], 'd_ckId': ['6aa779111c1b4ca77cff3648d9dee049'], 'd_curPage': ['0'], 'd_pageSize': ['40'], 'd_headId': ['6aa779111c1b4ca77cff3648d9dee049']}, '2018互联网300强': {'init': ['-1'], 'headckid': ['58d828c357a8cb19'], 'flushckid': ['1'], 'fromSearchBtn': ['2'], 'keyword': ['用户体验'], 'compTag': ['182'], 'ckid': ['58d828c357a8cb19'], 'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'], 'd_sfrom': ['search_unknown'], 'd_ckId': ['6aa779111c1b4ca77cff3648d9dee049'], 'd_curPage': ['0'], 'd_pageSize': ['40'], 'd_headId': ['6aa779111c1b4ca77cff3648d9dee049']}, '制造业500强': {'init': ['-1'], 'headckid': ['58d828c357a8cb19'], 'flushckid': ['1'], 'fromSearchBtn': ['2'], 'keyword': ['用户体验'], 'compTag': ['186'], 'ckid': ['58d828c357a8cb19'], 'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'], 'd_sfrom': ['search_unknown'], 'd_ckId': ['6aa779111c1b4ca77cff3648d9dee049'], 'd_curPage': ['0'], 'd_pageSize': ['40'], 'd_headId': ['6aa779111c1b4ca77cff3648d9dee049']}, 'AI创新成长50强 ': {'init': ['-1'], 'headckid': ['58d828c357a8cb19'], 'flushckid': ['1'], 'fromSearchBtn': ['2'], 'keyword': ['用户体验'], 'compTag': ['189'], 'ckid': ['58d828c357a8cb19'], 'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'], 'd_sfrom': ['search_unknown'], 'd_ckId': ['6aa779111c1b4ca77cff3648d9dee049'], 'd_curPage': ['0'], 'd_pageSize': ['40'], 'd_headId': ['6aa779111c1b4ca77cff3648d9dee049']}, '独角兽': {'init': ['-1'], 'headckid': ['58d828c357a8cb19'], 'flushckid': ['1'], 'fromSearchBtn': ['2'], 'keyword': ['用户体验'], 'compTag': ['130'], 'ckid': ['58d828c357a8cb19'], 'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'], 'd_sfrom': ['search_unknown'], 'd_ckId': ['6aa779111c1b4ca77cff3648d9dee049'], 'd_curPage': ['0'], 'd_pageSize': ['40'], 'd_headId': ['6aa779111c1b4ca77cff3648d9dee049']}, '上市公司': {'init': ['-1'], 'headckid': ['58d828c357a8cb19'], 'flushckid': ['1'], 'fromSearchBtn': ['2'], 'keyword': ['用户体验'], 'compTag': ['156'], 'ckid': ['58d828c357a8cb19'], 'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'], 'd_sfrom': ['search_unknown'], 'd_ckId': ['6aa779111c1b4ca77cff3648d9dee049'], 'd_curPage': ['0'], 'd_pageSize': ['40'], 'd_headId': ['6aa779111c1b4ca77cff3648d9dee049']}}\n",
    "\n",
    "# 上周C-1   多个页面准备测试1 中国500强\n",
    "url = \"https://www.liepin.com/zhaopin/\"\n",
    "session = HTMLSession()\n",
    "payload = 参数_compTag_用户体验['中国500强']\n",
    "r = session.get( url, params = payload)\n",
    "\n",
    "# r.url\n",
    "\n",
    "# 上周C-2  简化 A-1   单一页面爬+解析\n",
    "session = HTMLSession()\n",
    "\n",
    "def requests_liepin( url, params):\n",
    "    r = session.get( url , params = payload)\n",
    "\n",
    "    # 先取特定元素, 精准打击其子后辈\n",
    "    主要元素 = r.html.xpath( '//ul[@class=\"sojob-list\"]/li')\n",
    "\n",
    "    # 作为xpath字典，键为我要抓的牛肉名称，值为xpath\n",
    "    dict_xpaths={ \n",
    "        'text': {\n",
    "            'edu':      '//div[contains(@class,\"job-info\")]/p/span[@class=\"edu\"]',\n",
    "            '经验':      '//div[contains(@class,\"job-info\")]/p/span[@class=\"edu\"]/following-sibling::span',\n",
    "            '薪水':    '//div[contains(@class,\"job-info\")]/p/span[@class=\"text-warning\"]', \n",
    "            '时间':    '//div[contains(@class,\"job-info\")]/p/time/@title', \n",
    "            '职称':    '//div[contains(@class,\"job-info\")]/h3/a', \n",
    "            '公司地点': '//div[contains(@class,\"job-info\")]/p/a',\n",
    "            '公司名称': '//div[contains(@class,\"sojob-item-main\")]//p[@class=\"company-name\"]/a', \n",
    "        },\n",
    "        'text_content': {\n",
    "        },\n",
    "        'href': {\n",
    "            '链结':    '//div[contains(@class,\"job-info\")]/h3/a', \n",
    "            '公司URL': '//div[contains(@class,\"sojob-item-main\")]//p[@class=\"company-name\"]/a', \n",
    "        }\n",
    "    }\n",
    "\n",
    "    def get_e_text_content(_xpath_):\n",
    "        # 高级列表推导\n",
    "        暂存结果 = [e.xpath(_xpath_)[0].lxml.text_content() for e in 主要元素]\n",
    "        return(暂存结果)\n",
    "\n",
    "    def get_e_text(_xpath_):\n",
    "        # 高级列表推导\n",
    "        暂存结果 = [\"\".join([x.strip() if type(x) is str else x.text.strip() for x in e.xpath(_xpath_)]) for e in 主要元素]\n",
    "        return(暂存结果)\n",
    "\n",
    "    def get_e_href(_xpath_):\n",
    "        # 高级列表推导\n",
    "        暂存结果 = [list(e.xpath(_xpath_, first=True).absolute_links)[0] \\\n",
    "                   if len(e.xpath(_xpath_, first=True).absolute_links) >= 1  \\\n",
    "                   else \"\" for e in 主要元素]\n",
    "        return(暂存结果)\n",
    "\n",
    "    # 只对主要元素下进行.xpath取值\n",
    "    数据字典 = dict()\n",
    "\n",
    "    数据字典 = {k:get_e_text_content(v) for k,v in dict_xpaths['text_content'].items()}\n",
    "    数据字典.update({k:get_e_text(v) for k,v in dict_xpaths['text'].items()})\n",
    "    数据字典.update({k:get_e_href(v) for k,v in dict_xpaths['href'].items()})\n",
    "\n",
    "    数据 = pd.DataFrame(数据字典)\n",
    "    #数据.to_excel(\"20春_Web数据挖掘_week03_liepin.xlsx\", sheet_name=\"搜查结果\")\n",
    "    return (数据)\n",
    "\n",
    "\n",
    "# 上周C-3   多个页面\n",
    "url = \"https://www.liepin.com/zhaopin/\"\n",
    "\n",
    "list_df = list()\n",
    "for k,v in 参数_compTag_用户体验.items():\n",
    "    payload = v\n",
    "    df = requests_liepin( url, params = payload)\n",
    "    df = df.assign (热门公司类型 = k)    \n",
    "    list_df.append(df)\n",
    "\n",
    "df_all = pd.concat(list_df)\n",
    "df_all\n",
    "\n",
    "# 上周C-4   输出\n",
    "df_all.to_excel(\"20春_Web数据挖掘_week03_liepin_各热门公司类型.xlsx\", sheet_name=\"搜查结果\")\n",
    "\n",
    "# 上周C-5 Pandas  基本能力\n",
    "\n",
    "print (df_all.nunique())\n",
    "df_all[['edu']].drop_duplicates()\n",
    "\n",
    "df_all.groupby(['公司名称','edu']).agg({\"职称\":\"count\"}).sort_values(by='职称', ascending=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "-----\n",
    "\n",
    "## 本周实践目标\n",
    "<mark> 如何有系统的把更多页数据(相同结构)作系统性爬取 </mark>[猎聘PC版](https://www.liepin.com/zhaopin/)\n",
    "* 翻页：参数字典的拆解\n",
    "  * xpath解析翻页a/@href\n",
    "  * 建构参数模板\n",
    "  * 建构参数字典\n",
    "* 翻页：系统性迭代\n",
    "  * robots.txt\n",
    "  * 频率及时间\n",
    "* 翻页：数据备份与整合\n",
    "  * 储存备份\n",
    "  * 数据整合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 翻页：参数字典的拆解\n",
    "## xpath解析翻页a/@href （翻页链接）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-0   单一页面\n",
    "import requests_html\n",
    "from requests_html import HTMLSession\n",
    "\n",
    "url = \"https://www.liepin.com/zhaopin/?keyword=PRD\"\n",
    "session = HTMLSession()\n",
    "r = session.get( url ) # 获取页面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element 'a' class=('first', 'disabled') href='javascript:;' title='首页'>, <Element 'a' class=('disabled',) href='javascript:;'>, <Element 'a' class=('current',) href='javascript:;'>, <Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1'>, <Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=2'>, <Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=3'>, <Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=4'>, <Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1'>, <Element 'a' class=('last',) href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=9' title='末页'>]\n"
     ]
    }
   ],
   "source": [
    "# 试验\n",
    "xpath_翻页_a = '//div[@class=\"pagerbar\"]/a'  #先在页面找到翻页按钮的xpath\n",
    "print (r.html.xpath(xpath_翻页_a))  # 发现这些链接都很长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[<Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1'>, <Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=2'>, <Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=3'>, <Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=4'>, <Element 'a' href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1'>, <Element 'a' class=('last',) href='/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=9' title='末页'>]\n"
     ]
    }
   ],
   "source": [
    "# A-1  xpath 解析翻页a/@href\n",
    "# xpath_翻页_a = '//div[@class=\"pagerbar\"]/a' \n",
    "# 观察上方结果：class有disabled, current等href是javascript\n",
    "# 有共同点：href里有/zhaopin的是我们想要的\n",
    "\n",
    "xpath_翻页a = '//div[@class=\"pagerbar\"]/a[starts-with(@href,\"/zhaopin\")]' # 准确一点的写法\n",
    "                                       # [starts-with(@href,\"/zhaopin\")]   \n",
    "                                      #  以...开头的(通常这里是属性,\"链接\")\n",
    "print (r.html.xpath(xpath_翻页a)) # 按钮控件的链接"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1', '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=2', '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=3', '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=4', '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1', '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=9']\n"
     ]
    }
   ],
   "source": [
    "# 一个一个看\n",
    "href_列表 = [x.xpath('//@href')[0] for x in r.html.xpath(xpath_翻页a)]\n",
    "print (href_列表) # 取第一页含有的翻页href，其他不要"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['2', '3', '4', '5', '下一页', '']\n"
     ]
    }
   ],
   "source": [
    "# 一个一个看\n",
    "文字_列表 = [x.text for x in r.html.xpath(xpath_翻页a)]\n",
    "print (文字_列表) # 只取a标签的文本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'2': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1', '3': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=2', '4': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=3', '5': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=4', '下一页': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1', '': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=9'}\n"
     ]
    }
   ],
   "source": [
    "# 一个一个看\n",
    "href_字典 = {x.text:x.xpath('//@href')[0]  for x in r.html.xpath(xpath_翻页a)}\n",
    "print (href_字典) # 前面两个结合来看"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 观察：\n",
    "此网页是否给出开始丶步进丶及结束的信息，以方便我们完成迭代设置\n",
    "\n",
    "* 老问题 URL太长，用上周的URL+query参数解析与pandas数据框找到异同之处\n",
    "* 老问题 怎么系统化出URL？用上周的URL+query参数解析与pandas数据框找到异同之处的时候，顺便构建参数字典，至少让以下参数可控制\n",
    "  * 搜索关键词：上周keyword\n",
    "  * 页码在哪？\n",
    "* 实践挑战：如何把上周代码模块化为我们所用？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 6 entries, 0 to 5\n",
      "Data columns (total 6 columns):\n",
      " #   Column    Non-Null Count  Dtype \n",
      "---  ------    --------------  ----- \n",
      " 0   scheme    6 non-null      object\n",
      " 1   netloc    6 non-null      object\n",
      " 2   path      6 non-null      object\n",
      " 3   params    6 non-null      object\n",
      " 4   query     6 non-null      object\n",
      " 5   fragment  6 non-null      object\n",
      "dtypes: object(6)\n",
      "memory usage: 416.0+ bytes\n",
      "scheme      1\n",
      "netloc      1\n",
      "path        1\n",
      "params      1\n",
      "query       5\n",
      "fragment    1\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>scheme</th>\n",
       "      <th>netloc</th>\n",
       "      <th>path</th>\n",
       "      <th>params</th>\n",
       "      <th>query</th>\n",
       "      <th>fragment</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  scheme netloc       path params  \\\n",
       "0                /zhaopin/          \n",
       "1                /zhaopin/          \n",
       "2                /zhaopin/          \n",
       "3                /zhaopin/          \n",
       "4                /zhaopin/          \n",
       "\n",
       "                                               query fragment  \n",
       "0  init=-1&headckid=63477fba41564590&fromSearchBt...           \n",
       "1  init=-1&headckid=63477fba41564590&fromSearchBt...           \n",
       "2  init=-1&headckid=63477fba41564590&fromSearchBt...           \n",
       "3  init=-1&headckid=63477fba41564590&fromSearchBt...           \n",
       "4  init=-1&headckid=63477fba41564590&fromSearchBt...           "
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# URL+query参数解析与pandas数据框找到异同之处\n",
    "# 自己先尝试后的结果\n",
    "import pandas as pd\n",
    "from urllib.parse import urlparse, parse_qs\n",
    "[ urlparse(x) for x in href_字典.values()]  # 注意字符串是没有value属性的,应传入字典\n",
    "\n",
    "df = pd.DataFrame([ urlparse(x) for x in href_字典.values()])\n",
    "df.info()\n",
    "print(df.nunique()) # nunique() Return number of unique elements in the object.即返回的是唯一值的个数\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init             1\n",
      "headckid         1\n",
      "fromSearchBtn    1\n",
      "keyword          1\n",
      "ckid             1\n",
      "siTag            1\n",
      "d_sfrom          1\n",
      "d_ckId           1\n",
      "d_curPage        1\n",
      "d_pageSize       1\n",
      "d_headId         1\n",
      "curPage          5\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>init</th>\n",
       "      <th>headckid</th>\n",
       "      <th>fromSearchBtn</th>\n",
       "      <th>keyword</th>\n",
       "      <th>ckid</th>\n",
       "      <th>siTag</th>\n",
       "      <th>d_sfrom</th>\n",
       "      <th>d_ckId</th>\n",
       "      <th>d_curPage</th>\n",
       "      <th>d_pageSize</th>\n",
       "      <th>d_headId</th>\n",
       "      <th>curPage</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  init          headckid fromSearchBtn keyword                         ckid  \\\n",
       "0   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "1   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "2   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "3   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "4   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "\n",
       "                                           siTag         d_sfrom  \\\n",
       "0  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "1  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "2  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "3  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "4  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "\n",
       "                             d_ckId d_curPage d_pageSize  \\\n",
       "0  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "1  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "2  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "3  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "4  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "\n",
       "                           d_headId curPage  \n",
       "0  c3c8f3fe1c66c48312f7e234ac1ab278       1  \n",
       "1  c3c8f3fe1c66c48312f7e234ac1ab278       2  \n",
       "2  c3c8f3fe1c66c48312f7e234ac1ab278       3  \n",
       "3  c3c8f3fe1c66c48312f7e234ac1ab278       4  \n",
       "4  c3c8f3fe1c66c48312f7e234ac1ab278       1  "
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_a = pd.DataFrame([{k:v[0] for k,v in parse_qs(x).items()} for x in df['query'] ])\n",
    "print (df_a.nunique())\n",
    "df_a.head() #发现只有curpage不一样"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## 建构参数模板\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>scheme</th>\n",
       "      <th>netloc</th>\n",
       "      <th>path</th>\n",
       "      <th>params</th>\n",
       "      <th>query</th>\n",
       "      <th>fragment</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=63477fba41564590&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  scheme netloc       path params  \\\n",
       "0                /zhaopin/          \n",
       "1                /zhaopin/          \n",
       "2                /zhaopin/          \n",
       "3                /zhaopin/          \n",
       "4                /zhaopin/          \n",
       "5                /zhaopin/          \n",
       "\n",
       "                                               query fragment  \n",
       "0  init=-1&headckid=63477fba41564590&fromSearchBt...           \n",
       "1  init=-1&headckid=63477fba41564590&fromSearchBt...           \n",
       "2  init=-1&headckid=63477fba41564590&fromSearchBt...           \n",
       "3  init=-1&headckid=63477fba41564590&fromSearchBt...           \n",
       "4  init=-1&headckid=63477fba41564590&fromSearchBt...           \n",
       "5  init=-1&headckid=63477fba41564590&fromSearchBt...           "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "scheme      1\n",
      "netloc      1\n",
      "path        1\n",
      "params      1\n",
      "query       5\n",
      "fragment    1\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>init</th>\n",
       "      <th>headckid</th>\n",
       "      <th>fromSearchBtn</th>\n",
       "      <th>keyword</th>\n",
       "      <th>ckid</th>\n",
       "      <th>siTag</th>\n",
       "      <th>d_sfrom</th>\n",
       "      <th>d_ckId</th>\n",
       "      <th>d_curPage</th>\n",
       "      <th>d_pageSize</th>\n",
       "      <th>d_headId</th>\n",
       "      <th>curPage</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1</td>\n",
       "      <td>63477fba41564590</td>\n",
       "      <td>2</td>\n",
       "      <td>PRD</td>\n",
       "      <td>63477fba41564590°radeFlag=0</td>\n",
       "      <td>1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>c3c8f3fe1c66c48312f7e234ac1ab278</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  init          headckid fromSearchBtn keyword                         ckid  \\\n",
       "0   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "1   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "2   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "3   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "4   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "5   -1  63477fba41564590             2     PRD  63477fba41564590°radeFlag=0   \n",
       "\n",
       "                                           siTag         d_sfrom  \\\n",
       "0  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "1  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "2  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "3  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "4  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "5  1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "\n",
       "                             d_ckId d_curPage d_pageSize  \\\n",
       "0  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "1  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "2  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "3  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "4  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "5  c3c8f3fe1c66c48312f7e234ac1ab278         0         40   \n",
       "\n",
       "                           d_headId curPage  \n",
       "0  c3c8f3fe1c66c48312f7e234ac1ab278       1  \n",
       "1  c3c8f3fe1c66c48312f7e234ac1ab278       2  \n",
       "2  c3c8f3fe1c66c48312f7e234ac1ab278       3  \n",
       "3  c3c8f3fe1c66c48312f7e234ac1ab278       4  \n",
       "4  c3c8f3fe1c66c48312f7e234ac1ab278       1  \n",
       "5  c3c8f3fe1c66c48312f7e234ac1ab278       9  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init             1\n",
      "headckid         1\n",
      "fromSearchBtn    1\n",
      "keyword          1\n",
      "ckid             1\n",
      "siTag            1\n",
      "d_sfrom          1\n",
      "d_ckId           1\n",
      "d_curPage        1\n",
      "d_pageSize       1\n",
      "d_headId         1\n",
      "curPage          5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# A-2 建构参数模板：找到关键参数及参数结构\n",
    "\n",
    "# 需要的模块\n",
    "from urllib.parse import urlparse, parse_qs\n",
    "import pandas as pd\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "# 总体目标：输入 href_列表, 建构出参数字典\n",
    "\n",
    "# urlparse 解析后丢入数据框\n",
    "df = pd.DataFrame([ urlparse(x) for x in href_列表])\n",
    "df_aa = pd.DataFrame([{k:v[0] for k,v in parse_qs(x).items()} for x in df['query'] ])\n",
    "\n",
    "display(df)\n",
    "print(df.nunique())\n",
    "display(df_aa)\n",
    "print(df_aa.nunique())\n",
    "\n",
    "df_aa.curPage\n",
    "df_aa = df_aa.assign (curPage_int=df_aa.curPage.astype(int)) \n",
    "                                               # astype()函数可用于转化dateframe某一列的数据类型\n",
    "                                               # int:变成整数,只有当该列的字符串全是由纯数字构成时才可以这样写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 观察：\n",
    "* query\n",
    "* curPage 5次, 最大值9, 本页不算?\n",
    "\n",
    "-----\n",
    "\n",
    "## 建构参数模板：curPage\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "参数模板: {'init': ['-1'], 'headckid': ['63477fba41564590'], 'fromSearchBtn': ['2'], 'keyword': ['PRD'], 'ckid': ['63477fba41564590°radeFlag=0'], 'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'], 'd_sfrom': ['search_unknown'], 'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'], 'd_curPage': ['0'], 'd_pageSize': ['40'], 'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'], 'curPage': ['1']}\n",
      "href_字典: {'2': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1', '3': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=2', '4': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=3', '5': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=4', '下一页': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=1', '': '/zhaopin/?init=-1&headckid=63477fba41564590&fromSearchBtn=2&keyword=PRD&ckid=63477fba41564590°radeFlag=0&siTag=1B2M2Y8AsgTpgAmY7PhCfg%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=c3c8f3fe1c66c48312f7e234ac1ab278&d_curPage=0&d_pageSize=40&d_headId=c3c8f3fe1c66c48312f7e234ac1ab278&curPage=9'}\n"
     ]
    }
   ],
   "source": [
    "# A-2 建构参数模板：找到关键参数及参数结构\n",
    "\n",
    "def parse_url_qs_for_curPage (url):\n",
    "    six_parts = urlparse(url) \n",
    "    out = parse_qs(six_parts.query)\n",
    "    return (out)\n",
    "\n",
    "# 取一例做模板和字典对比来看\n",
    "参数模板 = parse_url_qs_for_curPage(href_列表[0]) \n",
    "print (\"参数模板:\",参数模板)\n",
    "\n",
    "print (\"href_字典:\",href_字典)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "9\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{0: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [0]},\n",
       " 1: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [1]},\n",
       " 2: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [2]},\n",
       " 3: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [3]},\n",
       " 4: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [4]},\n",
       " 5: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [5]},\n",
       " 6: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [6]},\n",
       " 7: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [7]},\n",
       " 8: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [8]},\n",
       " 9: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['用户体验'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [9]}}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# A-3 建构参数模板生成器：keyword curPage\n",
    "# 创建函数：参数模板生成\n",
    "def 参数模板生成(keyword, curPage):\n",
    "    参数 = 参数模板.copy()\n",
    "    参数['curPage'] = curPage\n",
    "    参数['keyword'] = keyword\n",
    "    return (参数)\n",
    "\n",
    "参数_keyword_用户体验_curPage = { \n",
    "    i:参数模板生成(curPage = [i], \\\n",
    "                  keyword = ['用户体验']) \\\n",
    "    for i,v in href_字典.items()\\\n",
    "    }\n",
    "\n",
    "# \\反斜杠：续行符（在行尾时）\n",
    "# print(参数_keyword_用户体验_curPage) # 结果显示：curPage=2 一直到 下一页\n",
    "                                      # 只生成本页有的额外翻页URL, 并没有推估到&curPage=9,也没有这页\n",
    "\n",
    "print (df_aa.curPage_int.min()) # 最小值只有1\n",
    "print (df_aa.curPage_int.max()) # 最大值只有9\n",
    "\n",
    "# 应该是 0 (本页)....9(最大值)\n",
    "\n",
    "参数_keyword_用户体验_curPage = { \n",
    "    i:参数模板生成(curPage = [i], \\\n",
    "                  keyword = ['用户体验']) \\\n",
    "    for i in range(0,df_aa.curPage_int.max()+1)\\\n",
    "    }\n",
    "参数_keyword_用户体验_curPage"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 翻页：系统性迭代\n",
    "\n",
    "## 爬亦有道\n",
    "* robots.txt 站长/网站拥有者给搜索引擎的\"道\"\n",
    "* 频率及时间\n",
    "  * 不要爬太快\n",
    "  * 尽量像\"人\"一样礼貌\n",
    "  * time.sleep\n",
    "  \n",
    "```python\n",
    "\n",
    "# 上周C-3   多个页面\n",
    "url = \"https://www.liepin.com/zhaopin/\"\n",
    "\n",
    "list_df = list()\n",
    "for k,v in 参数_compTag_用户体验.items():\n",
    "    payload = v\n",
    "    df = requests_liepin( url, params = payload)\n",
    "    df = df.assign (热门公司类型 = k)    \n",
    "    list_df.append(df)\n",
    "\n",
    "df_all = pd.concat(list_df)\n",
    "df_all\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# B-1 第三周C-2  简化 第二周A-1   单一页面爬+解析\n",
    "session = HTMLSession()\n",
    "\n",
    "def requests_liepin( url, params):\n",
    "    r = session.get( url , params = payload)\n",
    "\n",
    "    # 先取特定元素, 精准打击其子后辈\n",
    "    主要元素 = r.html.xpath( '//ul[@class=\"sojob-list\"]/li')\n",
    "\n",
    "    # 作为xpath字典，键为我要抓的牛肉名称，值为xpath\n",
    "    dict_xpaths={ \n",
    "        'text': {\n",
    "            'edu':      '//div[contains(@class,\"job-info\")]/p/span[@class=\"edu\"]',\n",
    "            '经验':      '//div[contains(@class,\"job-info\")]/p/span[@class=\"edu\"]/following-sibling::span',\n",
    "            '薪水':    '//div[contains(@class,\"job-info\")]/p/span[@class=\"text-warning\"]', \n",
    "            '时间':    '//div[contains(@class,\"job-info\")]/p/time/@title', \n",
    "            '职称':    '//div[contains(@class,\"job-info\")]/h3/a', \n",
    "            '公司地点': '//div[contains(@class,\"job-info\")]/p/a',\n",
    "            '公司名称': '//div[contains(@class,\"sojob-item-main\")]//p[@class=\"company-name\"]/a', \n",
    "        },\n",
    "        'text_content': {\n",
    "        },\n",
    "        'href': {\n",
    "            '链接':    '//div[contains(@class,\"job-info\")]/h3/a', \n",
    "            '公司URL': '//div[contains(@class,\"sojob-item-main\")]//p[@class=\"company-name\"]/a', \n",
    "        }\n",
    "    }\n",
    "\n",
    "    def get_e_text_content(_xpath_):\n",
    "        # 高级列表推导\n",
    "        暂存结果 = [e.xpath(_xpath_)[0].lxml.text_content() for e in 主要元素]\n",
    "        return(暂存结果)\n",
    "\n",
    "    def get_e_text(_xpath_):\n",
    "        # 高级列表推导\n",
    "        暂存结果 = [\"\".join([x.strip() if type(x) is str else x.text.strip() for x in e.xpath(_xpath_)]) for e in 主要元素]\n",
    "        return(暂存结果)\n",
    "\n",
    "    def get_e_href(_xpath_):\n",
    "        # 高级列表推导\n",
    "        暂存结果 = [list(e.xpath(_xpath_, first=True).absolute_links)[0] \\\n",
    "                   if len(e.xpath(_xpath_, first=True).absolute_links) >= 1  \\\n",
    "                   else \"\" for e in 主要元素]\n",
    "        return(暂存结果)\n",
    "\n",
    "    # 只对主要元素下进行.xpath取值\n",
    "    数据字典 = dict()\n",
    "\n",
    "    数据字典 = {k:get_e_text_content(v) for k,v in dict_xpaths['text_content'].items()}\n",
    "    数据字典.update({k:get_e_text(v) for k,v in dict_xpaths['text'].items()})\n",
    "    数据字典.update({k:get_e_href(v) for k,v in dict_xpaths['href'].items()})\n",
    "\n",
    "    数据 = pd.DataFrame(数据字典)\n",
    "    #数据.to_excel(\"20春_Web数据挖掘_week03_liepin.xlsx\", sheet_name=\"搜查结果\")\n",
    "    return (数据)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 爬亦有道- 不要爬太快\n",
    "\n",
    "- %%time \n",
    "\n",
    "%% 应按惯例从第一行开始（magics %% should start from the first line by convention）\n",
    "\n",
    "魔法命令（执行%magic可以查看关于各个命令的说明）都以%或%%开头，以%开头的为行命令，以%%开头的为单元命令。\n",
    "\n",
    "行命令只对命令所在的行有效，而单元命令则必须出现在单元的第一行，对整个单元的代码进行处理。\n",
    "\n",
    "- time.sleep"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 4.53 s\n"
     ]
    }
   ],
   "source": [
    "%%time \n",
    "#计时\n",
    "time.sleep(3+4*random()) #random() 0-1里选随机数 \n",
    "#这里总共3-7秒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 1min 3s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# B-2 多个页面，但放慢脚步 time.sleep\n",
    "import time\n",
    "from random import random\n",
    "\n",
    "url = \"https://www.liepin.com/zhaopin/\"\n",
    "\n",
    "list_df = list()\n",
    "for k,v in 参数_keyword_用户体验_curPage.items():\n",
    "    payload = v\n",
    "    df = requests_liepin( url, params = payload)\n",
    "    time.sleep(3+4*random())  #放慢脚步 3-7秒, 平均约5秒\n",
    "    df = df.assign (curPage = k)  # 区分  curPage\n",
    "    list_df.append(df)\n",
    "\n",
    "df_all = pd.concat(list_df).reset_index()\n",
    "df_all.index.name = '序'\n",
    "\n",
    "# 上周C-4   输出\n",
    "df_all.to_excel(\"20春_Web数据挖掘_week04_liepin_翻页.xlsx\",\\\n",
    "                sheet_name=\"用户体验\")\n",
    "\n",
    "# 预估时间: 5秒*10 =50\n",
    "# 预估数量: 40*10 =400"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 4.21 s\n"
     ]
    }
   ],
   "source": [
    "%%time \n",
    "# 多个页面+多个关键词\n",
    "time.sleep(3+4*random()) #random() 0-1里选随机数 \n",
    "#这里总共3-7秒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户体验 10\n",
      "UX 10\n",
      "Wall time: 2min 1s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# B-3 多个页面+多个关键词\n",
    "import time\n",
    "from random import random\n",
    "\n",
    "url = \"https://www.liepin.com/zhaopin/\"\n",
    "xpath_翻页a = '//div[@class=\"pagerbar\"]/a[starts-with(@href,\"/zhaopin\")]'\n",
    "\n",
    "keywords = ['用户体验','UX']\n",
    "list_df = list()\n",
    "\n",
    "## 第一页试探有多长的页面\n",
    "for key in keywords:\n",
    "    payload = 参数模板生成(keyword=[key], curPage=['0'])\n",
    "    df = requests_liepin( url, params = payload)\n",
    "    href_列表 = [x.xpath('//@href')[0] for x in r.html.xpath(xpath_翻页a)]\n",
    "    df = pd.DataFrame([ urlparse(x) for x in href_列表])\n",
    "    df_qs = pd.DataFrame([{k:v[0] for k,v in parse_qs(x).items()} for x in df['query'] ])\n",
    "    df_qs = df_qs.assign (curPage_int=df_qs.curPage.astype(int)) # 变成整数\n",
    "    长度 = df_qs.curPage_int.max()+1\n",
    "    参数_keyword_X_curPage = { \n",
    "        i:参数模板生成(curPage = [i], \\\n",
    "                      keyword = [key]) \\\n",
    "        for i in range(0,长度)\\\n",
    "        }\n",
    "    #print (参数_keyword_X_curPage)\n",
    "    print (key,长度)\n",
    "    \n",
    "    for k,v in 参数_keyword_X_curPage.items():\n",
    "        payload = v\n",
    "        df = requests_liepin( url, params = payload)\n",
    "        time.sleep(3+4*random())  #放慢脚步 3-7秒, 平均约5秒\n",
    "        df = df.assign (keyword = key)  # 增加一列，区分keyword    \n",
    "        df = df.assign (curPage = k)  # 增加一列，区分curPage    \n",
    "        list_df.append(df)\n",
    "        \n",
    "df_all = pd.concat(list_df).reset_index() # concat函数是在pandas底下的方法，可以将数据根据不同的轴作简单的融合\n",
    "                                          # 若要用到index列，要记得用reset_index（）去处理index，会重新按序排好index\n",
    "df_all.index.name = '序'\n",
    "\n",
    "df_all.to_excel(\"20春_Web数据挖掘_week04_liepin_翻页.xlsx\",\\\n",
    "                sheet_name=\"_\".join(keywords))\n",
    "# 预估时间: 2*5秒*10 =100\n",
    "# 预估数量: 2*40*10 =800"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 翻页：数据备份与整合\n",
    "多个页面+多个关键词执行时，若怕中断最好把每一页的df内容备份做中继"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "用户体验 10\n",
      "UX 10\n",
      "产品需求 10\n",
      "PRD 10\n",
      "Wall time: 3min 54s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# C-1 多个页面+多个关键词\n",
    "import time\n",
    "from random import random\n",
    "\n",
    "url = \"https://www.liepin.com/zhaopin/\"\n",
    "xpath_翻页a = '//div[@class=\"pagerbar\"]/a[starts-with(@href,\"/zhaopin\")]'\n",
    "\n",
    "keywords = ['用户体验','UX','产品需求','PRD']\n",
    "list_df = list()\n",
    "\n",
    "## 第一页试探有多长的页面\n",
    "for key in keywords:\n",
    "    payload = 参数模板生成(keyword=[key], curPage=['0']) # payload：有效载荷：关键信息\n",
    "    df = requests_liepin( url, params = payload)\n",
    "    href_列表 = [x.xpath('//@href')[0] for x in r.html.xpath(xpath_翻页a)]\n",
    "    df = pd.DataFrame([ urlparse(x) for x in href_列表])\n",
    "    df_qs = pd.DataFrame([{k:v[0] for k,v in parse_qs(x).items()} for x in df['query'] ])\n",
    "    df_qs = df_qs.assign (curPage_int=df_qs.curPage.astype(int)) # 变成整数\n",
    "    长度 = df_qs.curPage_int.max()+1\n",
    "    参数_keyword_X_curPage = { \n",
    "        i:参数模板生成(curPage = [i], \\\n",
    "                      keyword = [key]) \\\n",
    "        for i in range(0,长度)\\\n",
    "        }\n",
    "    #print (参数_keyword_X_curPage)\n",
    "    print (key,长度)\n",
    "    \n",
    "    for k,v in 参数_keyword_X_curPage.items():\n",
    "        payload = v\n",
    "        df = requests_liepin( url, params = payload)\n",
    "        time.sleep(3+4*random())  #放慢脚步 3-7秒, 平均约5秒\n",
    "        ## 备份\n",
    "        df.to_csv(\"data_out/20春_Web数据挖掘_week04_liepin_{key}_{k}.tsv\"\\\n",
    "                  .format(key=key, k=k), sep=\"\\t\", encoding=\"utf8\")  # \\t 横向制表符\n",
    "        \n",
    "        df = df.assign (keyword = key)  # 增加一列，区分keyword    \n",
    "        df = df.assign (curPage = k)  # 增加一列，区分curPage    \n",
    "        list_df.append(df)\n",
    "        \n",
    "df_all = pd.concat(list_df).reset_index()\n",
    "df_all.index.name = '序'\n",
    "\n",
    "df_all.to_excel(\"20春_Web数据挖掘_week04_liepin_翻页_4.xlsx\",\\\n",
    "                sheet_name=\"_\".join(keywords))\n",
    "# 预估时间: 4*5秒*10 =200\n",
    "# 预估数量: 4*40*10 =1600"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 本周练习\n",
    "\n",
    "* 开始试验各类参数的调整\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 尝试单一页面 更换keywords\n",
    "import requests_html\n",
    "from requests_html import HTMLSession\n",
    "\n",
    "url_1 = \"https://www.liepin.com/zhaopin/?key=界面设计\"\n",
    "session = HTMLSession()\n",
    "r = session.get( url )\n",
    "\n",
    "xpath_翻页b = '//div[@class=\"pagerbar\"]/a[starts-with(@href,\"/zhaopin\")]'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 尝试单一页面 增加行业xpath\n",
    "session = HTMLSession()\n",
    "\n",
    "def requests_liepin( url_1, params):\n",
    "    r = session.get( url_1, params = payload)\n",
    "    主要元素 = r.html.xpath( '//ul[@class=\"sojob-list\"]/li')\n",
    "\n",
    "    # 增加行业分类\n",
    "    dict_xpaths={ \n",
    "        'text': {\n",
    "            'edu':      '//div[contains(@class,\"job-info\")]/p/span[@class=\"edu\"]',\n",
    "            '经验':      '//div[contains(@class,\"job-info\")]/p/span[@class=\"edu\"]/following-sibling::span',\n",
    "            '薪水':    '//div[contains(@class,\"job-info\")]/p/span[@class=\"text-warning\"]', \n",
    "            '时间':    '//div[contains(@class,\"job-info\")]/p/time/@title', \n",
    "            '职称':    '//div[contains(@class,\"job-info\")]/h3/a', \n",
    "            '行业情况': '//div[contains(@class,\"company-info nohover\")]/p[@class=\"field-financing\"]/span',\n",
    "            '公司地点': '//div[contains(@class,\"job-info\")]/p/a',\n",
    "            '公司名称': '//div[contains(@class,\"sojob-item-main\")]//p[@class=\"company-name\"]/a', \n",
    "        },\n",
    "        'text_content': {\n",
    "        },\n",
    "        'href': {\n",
    "            '链接':    '//div[contains(@class,\"job-info\")]/h3/a', \n",
    "            '公司URL': '//div[contains(@class,\"sojob-item-main\")]//p[@class=\"company-name\"]/a', \n",
    "        }\n",
    "    }\n",
    "\n",
    "    def get_e_text_content(_xpath_):\n",
    "        暂存结果 = [e.xpath(_xpath_)[0].lxml.text_content() for e in 主要元素]\n",
    "        return(暂存结果)\n",
    "\n",
    "    def get_e_text(_xpath_):\n",
    "        暂存结果 = [\"\".join([x.strip() if type(x) is str else x.text.strip() for x in e.xpath(_xpath_)]) for e in 主要元素]\n",
    "        return(暂存结果)\n",
    "\n",
    "    def get_e_href(_xpath_):\n",
    "        暂存结果 = [list(e.xpath(_xpath_, first=True).absolute_links)[0] \\\n",
    "                   if len(e.xpath(_xpath_, first=True).absolute_links) >= 1  \\\n",
    "                   else \"\" for e in 主要元素]\n",
    "        return(暂存结果)\n",
    "\n",
    "    # 只对主要元素下进行.xpath取值\n",
    "    数据字典 = dict()\n",
    "\n",
    "    数据字典 = {k:get_e_text_content(v) for k,v in dict_xpaths['text_content'].items()}\n",
    "    数据字典.update({k:get_e_text(v) for k,v in dict_xpaths['text'].items()})\n",
    "    数据字典.update({k:get_e_href(v) for k,v in dict_xpaths['href'].items()})\n",
    "\n",
    "    数据 = pd.DataFrame(数据字典)\n",
    "    return (数据)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['/zhaopin/?init=-1&headckid=688997476e88c7c2&fromSearchBtn=2&ckid=688997476e88c7c2°radeFlag=0&key=%E7%95%8C%E9%9D%A2%E8%AE%BE%E8%AE%A1&siTag=VCwBanFNhXFdIGyIkvugHw%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=6cc5de66681e0be7c0939620ef6763f6&d_curPage=0&d_pageSize=40&d_headId=6cc5de66681e0be7c0939620ef6763f6&curPage=1', '/zhaopin/?init=-1&headckid=688997476e88c7c2&fromSearchBtn=2&ckid=688997476e88c7c2°radeFlag=0&key=%E7%95%8C%E9%9D%A2%E8%AE%BE%E8%AE%A1&siTag=VCwBanFNhXFdIGyIkvugHw%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=6cc5de66681e0be7c0939620ef6763f6&d_curPage=0&d_pageSize=40&d_headId=6cc5de66681e0be7c0939620ef6763f6&curPage=2', '/zhaopin/?init=-1&headckid=688997476e88c7c2&fromSearchBtn=2&ckid=688997476e88c7c2°radeFlag=0&key=%E7%95%8C%E9%9D%A2%E8%AE%BE%E8%AE%A1&siTag=VCwBanFNhXFdIGyIkvugHw%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=6cc5de66681e0be7c0939620ef6763f6&d_curPage=0&d_pageSize=40&d_headId=6cc5de66681e0be7c0939620ef6763f6&curPage=3', '/zhaopin/?init=-1&headckid=688997476e88c7c2&fromSearchBtn=2&ckid=688997476e88c7c2°radeFlag=0&key=%E7%95%8C%E9%9D%A2%E8%AE%BE%E8%AE%A1&siTag=VCwBanFNhXFdIGyIkvugHw%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=6cc5de66681e0be7c0939620ef6763f6&d_curPage=0&d_pageSize=40&d_headId=6cc5de66681e0be7c0939620ef6763f6&curPage=4', '/zhaopin/?init=-1&headckid=688997476e88c7c2&fromSearchBtn=2&ckid=688997476e88c7c2°radeFlag=0&key=%E7%95%8C%E9%9D%A2%E8%AE%BE%E8%AE%A1&siTag=VCwBanFNhXFdIGyIkvugHw%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=6cc5de66681e0be7c0939620ef6763f6&d_curPage=0&d_pageSize=40&d_headId=6cc5de66681e0be7c0939620ef6763f6&curPage=1', '/zhaopin/?init=-1&headckid=688997476e88c7c2&fromSearchBtn=2&ckid=688997476e88c7c2°radeFlag=0&key=%E7%95%8C%E9%9D%A2%E8%AE%BE%E8%AE%A1&siTag=VCwBanFNhXFdIGyIkvugHw%7EfA9rXquZc5IkJpXC-Ycixw&d_sfrom=search_unknown&d_ckId=6cc5de66681e0be7c0939620ef6763f6&d_curPage=0&d_pageSize=40&d_headId=6cc5de66681e0be7c0939620ef6763f6&curPage=9']\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>scheme</th>\n",
       "      <th>netloc</th>\n",
       "      <th>path</th>\n",
       "      <th>params</th>\n",
       "      <th>query</th>\n",
       "      <th>fragment</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=688997476e88c7c2&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=688997476e88c7c2&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=688997476e88c7c2&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=688997476e88c7c2&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=688997476e88c7c2&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td></td>\n",
       "      <td></td>\n",
       "      <td>/zhaopin/</td>\n",
       "      <td></td>\n",
       "      <td>init=-1&amp;headckid=688997476e88c7c2&amp;fromSearchBt...</td>\n",
       "      <td></td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  scheme netloc       path params  \\\n",
       "0                /zhaopin/          \n",
       "1                /zhaopin/          \n",
       "2                /zhaopin/          \n",
       "3                /zhaopin/          \n",
       "4                /zhaopin/          \n",
       "5                /zhaopin/          \n",
       "\n",
       "                                               query fragment  \n",
       "0  init=-1&headckid=688997476e88c7c2&fromSearchBt...           \n",
       "1  init=-1&headckid=688997476e88c7c2&fromSearchBt...           \n",
       "2  init=-1&headckid=688997476e88c7c2&fromSearchBt...           \n",
       "3  init=-1&headckid=688997476e88c7c2&fromSearchBt...           \n",
       "4  init=-1&headckid=688997476e88c7c2&fromSearchBt...           \n",
       "5  init=-1&headckid=688997476e88c7c2&fromSearchBt...           "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "scheme      1\n",
      "netloc      1\n",
      "path        1\n",
      "params      1\n",
      "query       5\n",
      "fragment    1\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>init</th>\n",
       "      <th>headckid</th>\n",
       "      <th>fromSearchBtn</th>\n",
       "      <th>ckid</th>\n",
       "      <th>key</th>\n",
       "      <th>siTag</th>\n",
       "      <th>d_sfrom</th>\n",
       "      <th>d_ckId</th>\n",
       "      <th>d_curPage</th>\n",
       "      <th>d_pageSize</th>\n",
       "      <th>d_headId</th>\n",
       "      <th>curPage</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1</td>\n",
       "      <td>688997476e88c7c2</td>\n",
       "      <td>2</td>\n",
       "      <td>688997476e88c7c2°radeFlag=0</td>\n",
       "      <td>界面设计</td>\n",
       "      <td>VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1</td>\n",
       "      <td>688997476e88c7c2</td>\n",
       "      <td>2</td>\n",
       "      <td>688997476e88c7c2°radeFlag=0</td>\n",
       "      <td>界面设计</td>\n",
       "      <td>VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-1</td>\n",
       "      <td>688997476e88c7c2</td>\n",
       "      <td>2</td>\n",
       "      <td>688997476e88c7c2°radeFlag=0</td>\n",
       "      <td>界面设计</td>\n",
       "      <td>VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1</td>\n",
       "      <td>688997476e88c7c2</td>\n",
       "      <td>2</td>\n",
       "      <td>688997476e88c7c2°radeFlag=0</td>\n",
       "      <td>界面设计</td>\n",
       "      <td>VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1</td>\n",
       "      <td>688997476e88c7c2</td>\n",
       "      <td>2</td>\n",
       "      <td>688997476e88c7c2°radeFlag=0</td>\n",
       "      <td>界面设计</td>\n",
       "      <td>VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-1</td>\n",
       "      <td>688997476e88c7c2</td>\n",
       "      <td>2</td>\n",
       "      <td>688997476e88c7c2°radeFlag=0</td>\n",
       "      <td>界面设计</td>\n",
       "      <td>VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw</td>\n",
       "      <td>search_unknown</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>6cc5de66681e0be7c0939620ef6763f6</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  init          headckid fromSearchBtn                         ckid   key  \\\n",
       "0   -1  688997476e88c7c2             2  688997476e88c7c2°radeFlag=0  界面设计   \n",
       "1   -1  688997476e88c7c2             2  688997476e88c7c2°radeFlag=0  界面设计   \n",
       "2   -1  688997476e88c7c2             2  688997476e88c7c2°radeFlag=0  界面设计   \n",
       "3   -1  688997476e88c7c2             2  688997476e88c7c2°radeFlag=0  界面设计   \n",
       "4   -1  688997476e88c7c2             2  688997476e88c7c2°radeFlag=0  界面设计   \n",
       "5   -1  688997476e88c7c2             2  688997476e88c7c2°radeFlag=0  界面设计   \n",
       "\n",
       "                                           siTag         d_sfrom  \\\n",
       "0  VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "1  VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "2  VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "3  VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "4  VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "5  VCwBanFNhXFdIGyIkvugHw~fA9rXquZc5IkJpXC-Ycixw  search_unknown   \n",
       "\n",
       "                             d_ckId d_curPage d_pageSize  \\\n",
       "0  6cc5de66681e0be7c0939620ef6763f6         0         40   \n",
       "1  6cc5de66681e0be7c0939620ef6763f6         0         40   \n",
       "2  6cc5de66681e0be7c0939620ef6763f6         0         40   \n",
       "3  6cc5de66681e0be7c0939620ef6763f6         0         40   \n",
       "4  6cc5de66681e0be7c0939620ef6763f6         0         40   \n",
       "5  6cc5de66681e0be7c0939620ef6763f6         0         40   \n",
       "\n",
       "                           d_headId curPage  \n",
       "0  6cc5de66681e0be7c0939620ef6763f6       1  \n",
       "1  6cc5de66681e0be7c0939620ef6763f6       2  \n",
       "2  6cc5de66681e0be7c0939620ef6763f6       3  \n",
       "3  6cc5de66681e0be7c0939620ef6763f6       4  \n",
       "4  6cc5de66681e0be7c0939620ef6763f6       1  \n",
       "5  6cc5de66681e0be7c0939620ef6763f6       9  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "init             1\n",
      "headckid         1\n",
      "fromSearchBtn    1\n",
      "ckid             1\n",
      "key              1\n",
      "siTag            1\n",
      "d_sfrom          1\n",
      "d_ckId           1\n",
      "d_curPage        1\n",
      "d_pageSize       1\n",
      "d_headId         1\n",
      "curPage          5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 先看看\n",
    "href_列表 = [x.xpath('//@href')[0] for x in r.html.xpath(xpath_翻页b)]\n",
    "print (href_列表)\n",
    "\n",
    "# urlparse 解析\n",
    "from urllib.parse import urlparse, parse_qs\n",
    "import pandas as pd\n",
    "from IPython.display import display, HTML\n",
    "\n",
    "\n",
    "df = pd.DataFrame([ urlparse(x) for x in href_列表])\n",
    "df_b = pd.DataFrame([{k:v[0] for k,v in parse_qs(x).items()} for x in df['query'] ])\n",
    "\n",
    "display(df)\n",
    "print(df.nunique())\n",
    "display(df_b)\n",
    "print(df_b.nunique())\n",
    "\n",
    "df_b.curPage\n",
    "df_b = df_b.assign (curPage_int=df_b.curPage.astype(int)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [0]},\n",
       " 1: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [1]},\n",
       " 2: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [2]},\n",
       " 3: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [3]},\n",
       " 4: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [4]},\n",
       " 5: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [5]},\n",
       " 6: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [6]},\n",
       " 7: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [7]},\n",
       " 8: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [8]},\n",
       " 9: {'init': ['-1'],\n",
       "  'headckid': ['63477fba41564590'],\n",
       "  'fromSearchBtn': ['2'],\n",
       "  'keyword': ['界面设计'],\n",
       "  'ckid': ['63477fba41564590°radeFlag=0'],\n",
       "  'siTag': ['1B2M2Y8AsgTpgAmY7PhCfg~fA9rXquZc5IkJpXC-Ycixw'],\n",
       "  'd_sfrom': ['search_unknown'],\n",
       "  'd_ckId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'd_curPage': ['0'],\n",
       "  'd_pageSize': ['40'],\n",
       "  'd_headId': ['c3c8f3fe1c66c48312f7e234ac1ab278'],\n",
       "  'curPage': [9]}}"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 建构参数模板\n",
    "def 参数模板生成(keyword, curPage):\n",
    "    参数 = 参数模板.copy()\n",
    "    参数['curPage'] = curPage\n",
    "    参数['keyword'] = keyword\n",
    "    return (参数)\n",
    "\n",
    "参数_keyword_界面设计_curPage = { \n",
    "    i:参数模板生成(curPage = [i], \\\n",
    "                  keyword = ['界面设计']) \\\n",
    "    for i in range(0,df_b.curPage_int.max()+1)\\\n",
    "    }\n",
    "参数_keyword_界面设计_curPage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['界面设计'] 页数: 10\n",
      "Wall time: 1min 1s\n"
     ]
    }
   ],
   "source": [
    "%%time \n",
    "#计时\n",
    "time.sleep(3+4*random()) #random() 0-1里选随机数 #这里总共3-7秒\n",
    "\n",
    "import time\n",
    "from random import random\n",
    "\n",
    "url_1 = \"https://www.liepin.com/zhaopin/?key=界面设计\"\n",
    "keywords = ['界面设计']\n",
    "\n",
    "list_df = list()\n",
    "for k,v in 参数_keyword_界面设计_curPage.items():\n",
    "    payload = v\n",
    "    df = requests_liepin( url_1, params = payload)\n",
    "    time.sleep(3+4*random())  #总共3-7秒, 平均约5秒\n",
    "    df = df.assign (curPage = k)  # 增加一列，区分curPage\n",
    "    list_df.append(df)\n",
    "\n",
    "df_all = pd.concat(list_df).reset_index()\n",
    "df_all.index.name = '序'\n",
    "\n",
    "# 输出\n",
    "df_all.to_excel(\"week04_liepin_翻页_UI.xlsx\",\\\n",
    "                sheet_name=\"界面设计\")\n",
    "\n",
    "页数 = df_b.curPage_int.max()+1\n",
    "print (key,\"页数:\",页数)\n",
    "# 预估时间: 5秒*10 =50\n",
    "# 预估数量: 40*10 =400"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "749px",
    "left": "1125.609375px",
    "top": "110px",
    "width": "281.390625px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
